home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / MacGnuGo 0.5e / gnugo.src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-02  |  6.6 KB  |  246 lines  |  [TEXT/R*ch]

  1. /*
  2. undo(int i) { return; }
  3. saveSmartGoFile(char* i) { return; }
  4. initHistory() { return; }
  5. addHistory(int i, int j, int k) { return; }
  6. */
  7. /*
  8.                 GNU GO - the game of Go (Wei-Chi)
  9.                 Version 1.1   last revised 3-1-89
  10.            Copyright (C) Free Software Foundation, Inc.
  11.                       written by Man L. Li
  12.                       modified by Wayne Iba
  13.                     documented by Bob Webber
  14. */
  15. /*
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation - version 1.
  19.  
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. GNU General Public License in file COPYING for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with this program; if not, write to the Free Software
  27. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28.  
  29. Please report any bug/fix, modification, suggestion to
  30.  
  31. mail address:   Man L. Li
  32.                 Dept. of Computer Science
  33.                 University of Houston
  34.                 4800 Calhoun Road
  35.                 Houston, TX 77004
  36.  
  37. e-mail address: manli@cs.uh.edu         (Internet)
  38.                 coscgbn@uhvax1.bitnet   (BITNET)
  39.                 70070,404               (CompuServe)
  40. */
  41.  
  42. #include <stdio.h>
  43.  
  44. #define EMPTY 0
  45. #define WHITE 1
  46. #define BLACK 2
  47.  
  48. unsigned char p[19][19], l[19][19], ma[19][19], ml[19][19];
  49. int mymove, umove;
  50. long int rd;
  51. int lib, play, pass;
  52. int mik, mjk, uik, ujk, mk, uk;  /* piece captured */
  53. int opn[9];  /* opening pattern flag */
  54.  
  55. /* added from NextGo */
  56. int MAXX, MAXY, blackCapturedKoI, blackCapturedKoJ, whiteCapturedKoI,
  57.  whiteCapturedKoJ, blackCaptured, whiteCaptured, currentStone, 
  58.  opposingStone, blackPassed, whitePassed;
  59.  
  60. int blackTerritory, whiteTerritory;
  61. int blackStones, whiteStones;
  62. int mx, my;
  63. int hcap2, hcap;
  64. unsigned char capturedmat[19][19];
  65. int rflag = 0;
  66. extern int lastMove;
  67. int randomize_flag = 0;
  68.  
  69. void initstuff()
  70. {
  71.    int i, j;
  72. /* init opening pattern numbers to search */
  73.       for (i = 0; i < 9; i++)
  74.         opn[i] = 1;
  75.       opn[4] = 0;
  76.  
  77. /* init board */
  78.       for (i = 0; i < 19; i++)
  79.         for (j = 0; j < 19; j++)
  80.           capturedmat[i][j] = p[i][j] = EMPTY;
  81.  
  82. /* init global variables */
  83.    mk = 0;  uk = 0;
  84.    blackCaptured = 0; whiteCaptured = 0;
  85.  
  86. /* init global variables */
  87.    play = 1;
  88.    pass = 0;
  89.    mik = -1; mjk = -1;
  90.    uik = -1; ujk = -1;
  91.    seed(&rd);   /* start random number seed */
  92. }
  93.  
  94. main(argc, argv)  int argc;  char *argv[];
  95. {
  96.    FILE *fp;
  97.    int i, j;
  98.    char ans[5];
  99.    char *fname;
  100.    int cont = 0;
  101.    int argn = 1;
  102.  
  103.    if (argc > argn && argv[argn][0] > '9') fname = argv[argn++];
  104.    else fname = NULL;
  105.  
  106. /* show instruction */
  107.    /* showinst(); */
  108.    printf("GnuGo, Copyright 1989,1993 Free Software Foundation, Inc.\n");
  109.  
  110. /* ask for board size */
  111.    if (argc > argn) sscanf(argv[argn++],"%d", &i);
  112.    else {
  113.       printf("board size (5 to 19)? ");
  114.       scanf("%d", &i);
  115.       getchar();
  116.    }
  117.    if (i < 5) i = 5; if (i > 19) i = 19;
  118.    MAXX = MAXY = i;
  119.  
  120.    initstuff();
  121.  
  122.    if (1)  /* new game */ {
  123. /* ask for handicap */
  124.       if (argc > argn) sscanf(argv[argn++],"%d", &hcap2);
  125.       else {
  126.         printf("\nNumber of handicap for black (0 to 17)? ");
  127.         scanf("%d", &hcap2);
  128.         getchar();
  129.       }
  130.  
  131. /* choose color */
  132.       if (argc > argn) sscanf(argv[argn++],"%c", ans);
  133.       else {
  134.         printf("\nChoose side(b or w)? ");
  135.         scanf("%c",ans);
  136.         getchar();
  137.         printf("\n");
  138.       }
  139.  
  140.       if (argc > argn) {
  141.         if (strcmp(argv[argn], "rand") == 0) { argn++; rflag = 1; }
  142.         else if (strcmp(argv[argn], "rand2") == 0) { argn++; rflag = 2; }
  143.         else if (strcmp(argv[argn], "eval3") == 0) { argn++; rflag = 3; }
  144.       }
  145.       if (hcap2 < 0) hcap2 = 0; hcap = hcap2; if (hcap > MAXX) hcap = MAXX;
  146.       if (hcap > 17) hcap = 17;
  147.       if (rflag > 0 && MAXX > 8) { hcap = hcap > 4 ? 4 : 0; }
  148.       sethand(hcap);
  149.       initHistory();
  150.  
  151.       if (ans[0] == 'b') {
  152.          mymove = WHITE;   /* computer white */
  153.          umove = BLACK;   /* human black */
  154.          currentStone = mymove; opposingStone = umove; 
  155.          if (hcap != 0 && hcap2 == hcap) {
  156.             genmove(&i, &j);   /* computer move */
  157.             p[i][j] = mymove;
  158.         addHistory(WHITE,i,j);
  159.          }
  160.       } else {
  161.          mymove = BLACK;   /* computer black */
  162.          umove = WHITE;   /* human white */
  163.          currentStone = mymove; opposingStone = umove; 
  164.          if (hcap == 0) {
  165.             genmove(&i, &j);   /* computer move */
  166.             p[i][j] = mymove;
  167.         addHistory(BLACK,i,j);
  168.          }
  169.       }
  170.     }
  171.  
  172. /* display game board */
  173.  
  174.    showboard(-1,-1);
  175.  
  176. /* main loop */
  177.    while (play > 0)
  178.      {
  179.       if (currentStone == BLACK && hcap2-1 > hcap) { i = j = -1; hcap++; }
  180.       else getmove(&i, &j);   /* read human move */
  181.       addHistory(opposingStone,i,j);
  182.       if (play > 0)
  183.     {
  184.      if (i >= 0) {  /* not pass */
  185.         p[i][j] = umove;
  186.         pass = 0;
  187.      }
  188.      examboard(mymove);     /* remove my dead pieces */
  189.      if (pass != 2)
  190.        {
  191.         if (currentStone == WHITE && hcap2-1 > hcap)
  192.           { i = j = -1; hcap++; }
  193.         else genmove(&i, &j);   /* computer move */
  194.             addHistory(currentStone,i,j);
  195.         mx = i; my = j;
  196.         if (i >= 0) {   /* not pass */
  197.            p[i][j] = mymove;
  198.            pass = 0;
  199.         } else pass++;
  200.         examboard(umove);   /* remove your dead pieces */
  201. #ifdef MAC
  202.             if (rflag && Button() ) getchar();
  203. #endif
  204.       }
  205.      showboard(mx,my);
  206.      /* printf(" %d pass = %d   ",lastMove,pass); */
  207.        }
  208.       if (pass >= 2 || (rflag > 0 && lastMove > 3*MAXX*MAXY/2))
  209.     play = 0;    /* both pass then stop game */
  210.     }
  211.  
  212.  if (play == 0)
  213.    {
  214. /* finish game and count pieces */
  215.     ans[0] = 'y';
  216.     /*
  217.     printf("Do you want to count score (y or n)? ");
  218.     scanf("%c",ans);
  219.     */
  220.     /* if (ans[0] == 'y') endgame(); */
  221.     if (ans[0] != 'n' && ans[0] != 'N') {
  222.       score_game_c();
  223.       display_mat();
  224.       printf("j; black=%3d  white=%3d\n", blackTerritory + whiteCaptured, 
  225.               whiteTerritory + blackCaptured);
  226.       i = blackTerritory + blackStones; j = whiteTerritory + whiteStones;
  227.       printf("%dx%d, %c, h=%d, black=%3d  white=%3d, m=%d, gnu=%c, %d \n",
  228.     MAXX, MAXY, (i>j) ? 'b' : 'w', hcap2, i, j, lastMove,
  229.     (currentStone == BLACK) ? 'b' : 'w', rflag);
  230.     }
  231.     if (fname != NULL) saveSmartGoFile(fname);
  232.   }
  233. }
  234. /* end main */
  235.  
  236. score_game_c()
  237. {
  238.   int i,j;
  239.   score_game();
  240.   blackStones = whiteStones = 0;
  241.   for (i = 0; i < MAXX; i++) for (j = 0; j < MAXY; j++) {
  242.     if (p[i][j] == BLACK) blackStones++;
  243.     if (p[i][j] == WHITE) whiteStones++;
  244.   }
  245. }
  246.